package com.zj.utils.utils;

import android.widget.Spinner;
import android.widget.SpinnerAdapter;

import androidx.annotation.NonNull;

/**
 * @author jiang zhu on 2019/10/30
 */
public class SpinnerUtils {

    /**
     * 根据值, 设置spinner默认选中:
     *
     * @param spinner 传入spinner控件
     * @param value   传入与之匹配的值
     */
    public static void setSpinnerItemSelectedByValue(Spinner spinner, @NonNull String value) {
        SpinnerAdapter apsAdapter = spinner.getAdapter(); //得到SpinnerAdapter对象
        if (apsAdapter != null) {
            int k = apsAdapter.getCount();
            for (int i = 0; i < k; i++) {
                if (value.trim().equals(apsAdapter.getItem(i).toString().trim())) {
                    spinner.setSelection(i, true);// 默认选中项
                    break;
                }
            }
        }
    }

}
